package com.example.myfirst.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.myfirst.dao.OrdersDao;
import com.example.myfirst.entity.Orders;
import com.example.myfirst.entity.ShopCart;
import com.example.myfirst.service.OrdersService;
import com.example.myfirst.utils.JsonConvertUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrdersService {
    @Autowired
    private OrdersDao ordersDao;

    @ApiModelProperty("生成一个订单")
    @Override
    public Boolean addOrders(Orders orders) {
        int i = ordersDao.insert(orders);
        return i > 0;
    }

    @ApiModelProperty("根据用户id查询订单")
    @Override
    public List<Orders> queryOrders(Integer uid) throws JsonProcessingException {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", uid);
        List<Orders> list = ordersDao.selectList(qw);
        for (Orders orders : list) {
            // 将json字符串转换为集合对象
            String goods = orders.getGoods();
            List<ShopCart> list1 = JsonConvertUtils.jsonToObject(goods, List.class);
            orders.setList(list1);
        }
        return list;
    }
}
